<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" inline label-width="80px">
      <el-form-item label="栏目名称">
        <el-input v-model="queryParams.categoryName" placeholder="请输入" size="small" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="mb8">
      <el-button type="primary" size="mini" @click="handleAdd">新增</el-button>
      <el-button type="danger" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
    </div>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" />
      <el-table-column label="栏目名称" prop="categoryName" />
      <el-table-column label="编码" prop="categoryCode" width="160" />
      <el-table-column label="排序" prop="orderNum" width="80" />
      <el-table-column label="状态" prop="status" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status==='0'?'success':'info'">{{ scope.row.status==='0'?'正常':'停用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="160">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="上级栏目">
          <el-input v-model="form.parentId" placeholder="无则填0" />
        </el-form-item>
        <el-form-item label="栏目名称">
          <el-input v-model="form.categoryName" />
        </el-form-item>
        <el-form-item label="编码">
          <el-input v-model="form.categoryCode" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="form.orderNum" :min="0" />
        </el-form-item>
        <el-form-item label="状态">
          <el-switch v-model="form.status" active-value="0" inactive-value="1" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="form.remark" />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="open=false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
  </template>

<script>
import { listCategory, getCategory, addCategory, updateCategory, delCategory } from '@/api/cms/category'

export default {
  name: 'CmsCategory',
  data() {
    return {
      loading: false,
      list: [],
      total: 0,
      ids: [],
      single: true,
      multiple: true,
      title: '',
      open: false,
      queryParams: { pageNum: 1, pageSize: 10, categoryName: undefined },
      form: { categoryId: undefined, parentId: 0, categoryName: '', categoryCode: '', orderNum: 0, status: '0', remark: '' }
    }
  },
  created() { this.getList() },
  methods: {
    getList() {
      this.loading = true
      listCategory(this.queryParams).then(res => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handleQuery() { this.queryParams.pageNum = 1; this.getList() },
    resetQuery() { this.resetForm('queryForm'); this.handleQuery() },
    handleSelectionChange(selection) { this.ids = selection.map(i=>i.categoryId); this.single = selection.length!==1; this.multiple = !selection.length },
    handleAdd() { this.title='新增栏目'; this.form = { parentId:0, categoryName:'', categoryCode:'', orderNum:0, status:'0', remark:'' }; this.open=true },
    handleUpdate(row) { getCategory(row.categoryId).then(res=>{ this.form = Object.assign({}, res.data); this.title='编辑栏目'; this.open=true }) },
    handleDelete(row) { const ids = row.categoryId ? row.categoryId : this.ids; this.$modal.confirm('确认删除选中数据吗？').then(()=> delCategory(ids)).then(()=>{ this.getList(); this.$modal.msgSuccess('删除成功') }).catch(()=>{}) },
    submitForm() {
      const req = this.form.categoryId ? updateCategory(this.form) : addCategory(this.form)
      req.then(()=>{ this.$modal.msgSuccess('操作成功'); this.open=false; this.getList() })
    }
  }
}
</script>

<style scoped>
.mb8 { margin-bottom: 8px; }
</style>


